//
//  WorkExperienceController.m
//  APJ Resource
//
//  Created by Richard Zhao on 2/18/14.
//  Copyright (c) 2014 APJ. All rights reserved.
//

#import "WorkExperienceController.h"
#import "WorkExperienceCell.h"
#import "WorkExperience.h"
#import "HRStore.h"

@implementation WorkExperienceController
{
    NSArray *datas;
}

- (id)initWithStaffId:(int)staffId
{
    self = [super init];
    
    if (self) {
        datas = [[HRStore sharedStore] workExperienceOfStaff:staffId];
    }
    
    return self;
}

- (id)init
{
    [NSException raise:@"WorkExperience init error" format:@"please call initWithStaffId."];
    return nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WorkExperienceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"work"];
    if(!cell){
        cell = [[[NSBundle mainBundle]loadNibNamed:@"WorkExperienceCell" owner:self options:nil] lastObject];
    }
    
    WorkExperience *item = [datas objectAtIndex:indexPath.row];
    cell.companyLabel.text = [NSString stringWithFormat:@"%@   %@", item.company, item.deptOrProject];
    cell.dutyLabel.text = item.jobDuty;
    cell.workLabel.text = item.workDetails;
    cell.commentsLabel.text = item.comments;
    
    NSDate *fromDate = [NSDate dateWithTimeIntervalSince1970:item.startDate];
    NSDate *endDate = [NSDate dateWithTimeIntervalSince1970:item.endDate];
    NSDateFormatter *formate = [[NSDateFormatter alloc] init];
    formate.dateFormat = @"MMM-YYYY";
    cell.periodLabel.text = [NSString stringWithFormat:@"%@ --- %@", [formate stringFromDate:fromDate], [formate stringFromDate:endDate]];
    
    cell.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 246;
}
@end
